const app = getApp()
var CONFIG = require("../../../../config/config.js");
var share = require('../../../../utils/share.js');
// const ctx = wx.createCanvasContext('myCanvas');
Page({
    data: {
        discount_name: '',
        stu_id: '', //选课表ID
        discounts: [],
        discount_index: 0,
        discountsPrice: '',
        discountsId: '',
        // moneyfree: [{ index: 0, title: "请选择" }, { index: 1, title: "微信银行卡" }, { index: 2, title: "现金" }, { index: 3, title: "余额" }],
        moneyfree: [{ index: 0, title: "请选择" }, { index: 3, title: "小七币" }, { index: 2, title: "人民币" }],
        moneyfree_index: 0,
        area_team: [], //校区列表
        area_index: 0, //校区下标
        area_id: '', //校区下标
        title: '',
        text: '',
        imagePath: '',
        imgUrl: CONFIG.API_PIC,
        showerca: false,
        showerca1: false,
        butfalse: true,
        showfalse: false,
        showfalse1: false,
        text1: '',
        textsan: '请填写备注',
        pid: '',
        cate_id: '', //分类id
        course_id: '', //课程id
        specs_id: '', //规格id
        multiIndex: [-1, -1], //课程分类下标
        multiArray: [], //课程分类数组
        chooselist: [], //课程分类二级列表
        provinceslist: [], //课程分类列表
        cart_title: '', //课程分类选中的
        courseregin1: '', //课程
        courseregin2: '', //课程
        courseIndex: [-1, -1], //课程下标
        courseArray: [], //课程数组
        courselist: [], //当前规格数组
        course_title: '', //课程选中name
        gge_title: '',
        ggelist: '', //当前规格数组
        y_price: 0, //课程原价
        ggetitle: '', //选中规格name
        is_specs: 0, //0没有规格 1有规格
        pay_type: 1, //1一次付清 2分期
        downpayment: 0, //首付金额
        ye: '', //余款
        uc: '', //信息
        bank_index: 0,
        bankarr: [],
        newconid: '',
        keycourse: '',
        steparr: [{ id: 1, title: '1.课程分类选择' }, { id: 2, title: '2.选择课程' }, { id: 3, title: '3.选择优惠' }],
        is_stepid: 1,
        is_search: 1, //1搜索 2选择
        chooselist: [], //分类二级数据
        regin1: '',
        regin2: '',
        is_scorder: 1, //1还没有生成订单 2生成订单
        keywordlists: [],
        is_searchid: 1, //1分类搜索 2查询
        is_area_more: 1,
        childtype:'',
        cate_id1:'',
        photo:'',
        is_photo:0,
        isAllow:2,
        specsList:[],
        attrList:[],
        isspes:false,
        attrInfo:{
            id:'',
            frist_name:'',
            second_name:'',
            price:0
        },
        product:[],
        repeat_price:'',
        frist_nameN:'',
        pPrice:0,
        is_train:0,
        islink:true,
        childList:[]
    },
    areaeChangemore() {
        var that = this;
        that.setData({
            is_area_more: 2
        })
    },
    areaeChangemore1() {
        var that = this;
        that.setData({
            is_area_more: 1
        })
    },
    closeareae() {
        var that = this;
        let attrInfo={
            id:'',
            frist_name:'',
            second_name:'',
            price:0
        }
        that.setData({
            area_index: 0,
            area_id: '',
            courseIndex: [-1, -1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: 2,
            is_scorder: 1,
            is_stepid: 1,
            is_search: 1,
            is_searchid:1,
            attrInfo:attrInfo,
            product:[],
            repeat_price:'',
            frist_nameN:'',
            pPrice:0,
            is_train:0
        })
    },
    // 校区选择
    areaeChange(e) {
        var that = this;
        // console.log(e)
        var index = e.target.dataset.index;
        that.setData({
            area_index: index,
        })

        if (Number(index) > 0) {
            that.setData({
                area_id: that.data.area_team[index].id
            })
            that.getAreaCourse();

        } else {
            that.setData({
                area_id: ''
            })
        }
        that.setData({
            courseIndex: [-1,-1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: 2,
        })
    },
    // 获取校区列表
    teamlist() {
        var that = this;
        share.wxRequest('crmapi/User/team', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{
                    id: 0,
                    title: '请选择校区'
                }]
                var data1 = obj.concat(data);
                that.setData({
                    area_team: data1
                })
            }
        })
    },

    // 选择退款方式
    moneyfreeChange(e) {
        var that = this;
        that.setData({
            moneyfree_index: e.detail.value,
        })
    },

    // 选择开户行
    bankChange(e) {
        var that = this;
        that.setData({
            bank_index: e.detail.value,
        })
    },

    // 开户行
    banks() {
        var that = this;
        share.wxRequest('crmapi/user/banks', {}, function(res) {
            // console.log(res);
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{ title: "请选择", bank_code: 0 }];
                var methods = obj.concat(data);
                that.setData({
                    bankarr: methods
                })
            }
        })
    },

    //生命周期函数--监听页面加载
    onLoad: function(options) {
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        let that = this;
        if (options.newconid) {
            var newconid = options.newconid;
        } else {
            var newconid = '';
        }
        that.setData({
            stu_id: options.stu_id,
            newconid: newconid
                // stu_id: 56,
        });
        wx.showLoading({
                title: '加载中~',
            })
            // that.getAreaCourse();
        that.banks();
        that.teamlist();

        // 获取换课信息
        var obj = {
            uc_id: that.data.stu_id
        }
        if (Number(app.globalData.identity) == 3) {
            if (that.data.newconid != '' && Number(that.data.newconid) > 0) {
                var url = 'crmapi/user/userCourse'
            } else {
                var url = 'crmapi/Learn/userCourse'
            }
        } else {
            var url = 'crmapi/user/userCourse'
        }
        share.wxRequest(url, obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                // 缴费类型
                var data = res.data.data;
                let isAllow=0
                if(data.cate_id==162){
                    isAllow=1
                    // wx.showToast({
                    //     title: '技能提升课程不支持换课',
                    //     icon: 'none',
                    //     duration: 1500
                    // })
                }else{
                    isAllow=0
                }
                that.setData({
                    isAllow:isAllow,
                    uc: data,
                    ye: Math.round((data.discounts - data.account_paid) * 1000000) / 1000000
                });
            }
            wx.hideLoading();
        })

    },

    // 获取课程分类
    getAreaCourse() {
        var that = this;
        that.setData({
            multiIndex: [-1, -1], //课程分类下标
            multiArray: [], //课程分类数组
            chooselist: [],
            regin1: '',
            regin2: '',
        })
        share.wxRequest('crmapi/Market/cateList', { area_id: that.data.area_id }, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data1 = res.data.data.lists;
                that.setData({
                    multiArray: data1,
                })
            }
            wx.hideLoading();
        })
    },

    catedelChange(e) {
        var that = this;
        let attrInfo={
            id:'',
            frist_name:'',
            second_name:'',
            price:0
        }
        var index = e.target.dataset.index || e.currentTarget.dataset.index;
        if (Number(index) == 1) {
            that.setData({
                regin1: '',
                regin2: '',
                chooselist: [],
                "multiIndex[0]": -1,
                "multiIndex[1]": -1,
                cate_id: '',
                cate_id1:'',
                cart_title: '',
                courseIndex: [-1, -1],
                courseregin1: '',
                courseregin2: '',
                course_title: '',
                moneyfree_id: 2,
                is_xuancate: 1,
                is_stepid: 1,
                attrInfo:attrInfo,
                product:[],
                repeat_price:'',
                frist_nameN:'',
                pPrice:0,
                is_train:0
            })
        } else if (Number(index) == 2) {
            that.setData({
                cart_title: that.data.regin1,
                regin2: '',
                cate_id: '',
                // cate_id1:'',
                "multiIndex[1]": -1,
                is_xuancate: 1,
                is_stepid: 1,
                courseIndex: [-1, -1],
                courseregin1: '',
                courseregin2: '',
                course_title: '',
                attrInfo:attrInfo,
                product:[],
                repeat_price:'',
                frist_nameN:'',
                pPrice:0,
                is_train:0
            })
        } else if (Number(index) == 3) {
            if(that.data.childList==0){
                that.setData({
                    // courselist: [],
                    discount_name: '',
                    pay_type: '',
                    downpayment: '',
                    course_id: '',
                    discountsId: '',
                    discountsIndex: -1,
                    y_price: '',
                    course_title: '',
                    courseregin1: '',
                    "courseIndex[0]": -1,
                    "courseIndex[1]": -1,
                    is_stepid: 2,
                    attrInfo:attrInfo,
                    product:[],
                    repeat_price:'',
                    frist_nameN:'',
                    pPrice:0,
                    is_train:0
                })

            }else{
                that.setData({
                    courselist: [],
                    discount_name: '',
                    pay_type: '',
                    downpayment: '',
                    course_id: '',
                    discountsId: '',
                    discountsIndex: -1,
                    y_price: '',
                    course_title: '',
                    courseregin1: '',
                    "courseIndex[0]": -1,
                    "courseIndex[1]": -1,
                    is_stepid: 1,
                    attrInfo:attrInfo,
                    product:[],
                    repeat_price:'',
                    frist_nameN:'',
                    pPrice:0,
                    is_train:0
                })
            }
        }
        that.setData({
            is_scorder: 1,
        })
    },
    changecourseall() {
        var that = this;
        that.setData({
            regin1: '',
            regin2: '',
            chooselist: [],
            "multiIndex[0]": -1,
            "multiIndex[1]": -1,
            cate_id: '',
            cate_id1:'',
            cart_title: '',
            courseIndex: [-1, -1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: 1,
            is_stepid: 1,
            is_scorder: 1,
        })
    },
    // 选择课程分类
    catefirstChange(e) {
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        if (item.child && Number(item.child.length) > 0) {
            var child = item.child;
            var cate_id = '';
            var is_xuancate = 1;
            var is_stepid = 1;
        } else {
            var child = [];
            var cate_id = item.id;
            var is_xuancate = 2;
            var is_stepid = 2;
        }
        var cate_id1=item.id
        that.setData({
            regin1: item.title,
            chooselist: item.child,
            "multiIndex[0]": index,
            "multiIndex[1]": -1,
            cate_id: cate_id,
            cate_id1:cate_id1,
            cart_title: item.title,
            courseIndex: [-1, -1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: is_xuancate,
            is_stepid: is_stepid,
            childList:child
        })
        if (cate_id != '') {
            that.cateCourse();
        }
    },
    // /选择二级分类
    catesecChange(e) {
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        var cart_title = that.data.cart_title + ',' + item.title;
        that.setData({
            cart_title: cart_title,
            regin2: item.title,
            cate_id: item.id,
            "multiIndex[1]": index,
            is_xuancate: 2,
            is_stepid: 2,
        })
        that.cateCourse();
    },
    catesecChange1(e){
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        let regin1=that.data.regin1
        let cate_id=this.data.cate_id
        that.setData({
            cart_title: regin1+item.title,
            // regin2: item.title,
            cate_id:cate_id==item.id?'':item.id,
            "multiIndex[1]": index,
            is_xuancate: 2,
            is_stepid: 1,
        })
        if(cate_id!=item.id){
            that.cateCourse();
        }
    },
    courseChange(e){
        let that=this
        let item=e.target.dataset.item
        that.setData({
            course_id: item.course_id,
            course_title: item.course_name,
            courseregin1: item.course_name,
        })
        if(that.data.cate_id1=='162'){
            that.courseDetail1(item.course_id)

        }else{
            that.courseDetail(item.course_id)
        }
    },
    // 选择课程
    choosecourseclick(e) {
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        var is_photo=e.target.dataset.item.is_photo
        var data = []
        if (Number(item.id) != Number(that.data.uc.course_id)) {
            if(that.data.cate_id1==162){

            }else{
                data = [
                        {
                        discount_title: '无折扣',
                        discount_price: item.price,
                        discount_id: '',
                        type:that.data.childtype
                    }
                ]
            }
            // var data = [{
            //     discount_title: '无折扣',
            //     discount_price: item.price,
            //     discount_id: '',
            // }]
            if (item.child && Number(item.child.length) > 0) {
                var child = item.child;
                var cate_id = '';
                var is_xuancate = 1;
                var is_stepid = 1;
            } else {
                var child = [];
            }
            that.setData({
                courselist: data.concat(child),
                discount_name: '',
                pay_type: item.pay_type,
                downpayment: item.downpayment,
                course_id: item.id,
                discountsId: '',
                discountsIndex: -1,
                y_price: item.price,
                discountsPrice: item.price,
                course_title: item.title,
                courseregin1: item.title,
                "courseIndex[0]": index,
                "courseIndex[1]": -1,
                is_stepid: 3,
                is_photo:is_photo
            })
            if (Number(that.data.is_searchid) == 2) {
                if (item.pptitle != '') {
                    var cart_title = item.ptitle + ',' + item.pptitle
                } else {
                    var cart_title = item.ptitle
                }
                that.setData({
                    cate_id: item.pid,
                    cart_title: cart_title,
                })
            }
        } else {
            wx.showToast({
                title: '不能选择与原课程一样的课程',
                icon: 'none',
                duration: 1500
            })
        }

    },
    courseChange(e){
        let that=this
        let item=e.target.dataset.item
        that.setData({
            course_id: item.course_id,
            course_title: item.course_name,
            courseregin1: item.course_name,
        })
        if(that.data.cate_id1=='162'){
            that.courseDetail1(item.course_id)

        }else{
            that.courseDetail(item.course_id)
        }
    },
    courseDetail(id) {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var formData = {
            course_id: id,
        }
        share.wxRequest('crmapi/Market/courseAttr', formData, function(res) {
            if (Number(res.data.errcode) == 0) {
                let data=res.data.data.attr
                let arr=[]
                let specName=''
                let arr1=[]
                let specName1=''
                let specsList=[]
                data.forEach(val=>{
					if(arr.includes(val.frist_name)){				
					}else{
						arr.push(val.frist_name)	
					}
					if(arr1.includes(val.second_name)){				
					}else{
						arr1.push(val.second_name)	
					}
					if(val.frist_attr_name){
						specName=val.frist_attr_name
					}
					if(val.second_attr_name){
						specName1=val.second_attr_name
					}
				})
                if(arr.length!=0&&specName!=''){
					let obj={
						specName:specName,
						spec:[],
					}
					arr.forEach(v=>{
						let objs={
								name:v,
						}
						obj.spec.push(objs)
					})
					specsList[0]=obj
				}
				if(arr1.length!=0&&specName1!=''){
					let obj={
						specName:specName1,
						spec:[],
					}
					arr1.forEach(v=>{
						let objs={
								name:v,
						}
						obj.spec.push(objs)
					})
					specsList[1]=obj
				}
                that.setData({
                    // is_stepid:3,
                    isspes:true,
                    specsList: specsList,
                    attrList:data
                })
            }
            wx.hideLoading()
        })
    },
    courseDetail1(id) {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var formData = {
            course_id: id,
        }
        share.wxRequest('crmapi/Market/courseAttr', formData, function(res) {
            if (Number(res.data.errcode) == 0) {
                let data=res.data.data.attr
                data.forEach(v=>{
                    v.disCount=0
                })
                that.setData({
                    is_stepid:3,
                    attrList:data
                })
            }
            wx.hideLoading()
        })
    },
    getDisCount(e){
        let attrList=this.data.attrList
        let index=e.target.dataset.index
        let disCount=e.target.dataset.type
        let oldDisCount=attrList[index].disCount
        let product=[]
        let attrInfo =this.data.attrInfo
        let is_train =this.data.is_train
        if(oldDisCount==disCount&&oldDisCount!=0){
            attrList[index].disCount=0
        }else{
            attrList[index].disCount=disCount
        }
        attrList.forEach(v=>{
            if(v.disCount!=0){
                let obj={
                    id:v.id,
                    is_type:v.disCount
                }
                product.push(obj)
            }
        })
        let repeat_price=0
        let frist_nameN=''
        let pPrice=''
        let y_price=''
        attrList.forEach(v=>{
            if(v.disCount==2||v.disCount=='2'){
                repeat_price=Number(repeat_price)+Number(v.repeat_price)
            }
            if(v.disCount!=0&&v.disCount!='0'){
                frist_nameN=frist_nameN+v.frist_name,
                pPrice=Number(pPrice)+Number(v.price)
            }
        })
        this.setData({
            product:product,
            attrList:attrList,
            repeat_price:repeat_price,
            frist_nameN:frist_nameN,
            pPrice:pPrice,
            isspes:false
        })
        if(product.length>0){
            this.getLength()
        }
    },
    getLength(){
        // if(this.data.product.length==1){
        //     if(this.data.product[0].is_type==3){
        //         wx.showToast({
        //             title: '往期已报不可单独选择',
        //             icon: 'none'
        //         })
        //     }else{
        //         this.checkAttrRestrict()
        //     }
        // }else{
            this.checkAttrRestrict()
        // }
    },
    checkAttrRestrict() {
        var that = this;
        // wx.showLoading({
        //     title: '加载中~',
        // })
        var formData = {
            course_id: that.data.course_id,
            pid:that.data.cate_id,
            product:that.data.product
        }
        share.wxRequest('crmapi/Market/checkAttrRestrict', formData, function(res) {
                let attrInfo={}
                let repeat_price=that.data.repeat_price
                let frist_nameN=that.data.frist_nameN
                let pPrice=that.data.pPrice
                let attrList=that.data.attrList
                let is_train=that.data.is_train
                if(res.data.msg=='连报不存在'||res.data.msg=='错误'){
                    if(that.data.product.length==1){
                        attrInfo.id=formData.product[0].id
                        is_train=formData.product[0].is_type
                        attrInfo.frist_name=frist_nameN
                        attrInfo.second_name=''
                        attrInfo.price=Number(pPrice)+Number(repeat_price)
                        that.setData({
                            attrInfo:attrInfo,
                            is_train:is_train,
                            islink:false
                        })
                    }else{
                        let ids=[]
                        let a=0
                        formData.product.forEach(v=>{
                            if(v.is_type==3){
                                a++
                            }else{
                                ids.push(v.id)
                            }
                        })
                        // if(a==0){
                            let id=ids.join(',')
                            attrInfo.id=id
                            attrInfo.frist_name=frist_nameN
                            attrInfo.second_name=''
                            attrInfo.price=Number(pPrice)+Number(repeat_price)
                            that.setData({
                                attrInfo:attrInfo,
                                is_train:4,
                                islink:false
                            })
                        // }else{
                        //     attrList.forEach(v=>{
                        //         v.disCount=0
                        //     })
                        //     that.setData({
                        //         isspes:false,
                        //         attrInfo:attrInfo,
                        //         frist_nameN:'',
                        //         repeat_price:0,
                        //         pPrice:0,
                        //         product:[],
                        //         attrList:attrList
                        //     })
                        //     wx.showToast({
                        //         title: '当前不允许选择往期已报,请重新选择折扣',
                        //         icon: 'none'
                        //     })

                        // }
                        
                    }
                }
                if(res.data.msg.indexOf('名额已满')!=-1){
                    // 需要用户重新选
                    attrList.forEach(v=>{
                        v.disCount=0
                    })
                    that.setData({
                        isspes:false,
                        attrInfo:attrInfo,
                        frist_nameN:'',
                        repeat_price:0,
                        pPrice:0,
                        product:[],
                        attrList:attrList,
                        islink:false,
                        is_train:0
                    })
                    wx.showToast({
                        title: res.data.msg+',请重新选择折扣',
                        icon: 'none'
                    })
                }
                if(res.data.errcode==0){
                    attrInfo.id=res.data.data.id
                    attrInfo.frist_name=res.data.data.frist_name
                    attrInfo.second_name=''
                    attrInfo.price=Number(res.data.data.price)+Number(repeat_price)
                    that.setData({
                        // isspes:true,
                        attrInfo:attrInfo,
                        islink:true,
                        is_train:3
                    })
                }
            // wx.hideLoading()
        })
    },
    getspecs(e){
        let item=e.target.dataset.item
        let specsList=this.data.specsList
        let attrInfo=this.data.attrInfo
        let attrList=this.data.attrList
        let arr=[]
        specsList.forEach((v,index)=>{
            v.spec.forEach(val=>{
                if(val.name==item.name){
                    if(index==0){
                        attrInfo.id=''
                        attrInfo.price=0
                        attrInfo.frist_name=item.name
                        attrInfo.second_name=''
                        attrList.forEach(a=>{
                            if(a.frist_name==attrInfo.frist_name){
                                console.log(a.second_name);
                                let obj={
                                    name:a.second_name
                                }
                                arr.push(obj)
                            }
                        })
                        if(specsList.length==2){
                            specsList[1].spec=arr
                        }
                    }
                    if(index==1){
                        attrInfo.second_name=item.name
                    }
                }else{
                }
            })
        })
        attrList.forEach(a=>{
            if(specsList.length>=2){
                if(attrInfo.frist_name==a.frist_name&&attrInfo.second_name==a.second_name){
                    attrInfo.id=a.id
                    attrInfo.price=a.price
                }
            }else{
                if(attrInfo.frist_name==a.frist_name){
                    attrInfo.id=a.id
                    attrInfo.price=a.price
                }

            }
        })
        this.setData({
            specsList: specsList,
            attrInfo: attrInfo,
            isspes:true,
            discountsId:attrInfo.id,
            discountsPrice:attrInfo.price
        })
    },
    closeSpecs(){
        let attrInfo={
            id:'',
            frist_name:'',
            second_name:'',
            price:0
        }
        this.setData({
            isspes:false,
            attrInfo:attrInfo,
            product:[],
            repeat_price:'',
            frist_nameN:'',
            pPrice:0,
            is_train:0
        })

    },
    ordersclick() {
        var that = this;
        console.log('点击生成订单');
        if(this.data.product.length==1){
            if(this.data.product[0].is_type==3){
                wx.showToast({
                    title: '往期已报不可单独选择',
                    icon: 'none'
                })
            }else{
                that.setData({
                    is_scorder: 2,
                    isspes:false
                })
            }
        }else{
            let a=0
            this.data.product.forEach(v=>{
                if(v.is_type==3){
                    a++
                }
            })
            if(this.data.islink){
                that.setData({
                    is_scorder: 2,
                    isspes:false
                })
            }else{
                if(a==0){
                    that.setData({
                        is_scorder: 2,
                        isspes:false
                    })
                }else{
                    let attrList=that.data.attrList
                    attrList.forEach(v=>{
                         v.disCount=0
                    })
                    let attrInfo={}
                    that.setData({
                        isspes:false,
                        attrInfo:attrInfo,
                        frist_nameN:'',
                        repeat_price:0,
                        pPrice:0,
                        product:[],
                        attrList:attrList
                    })
                    wx.showToast({
                        title: '当前不允许选择往期已报,请重新选择折扣',
                        icon: 'none'
                    })

                }

            }
        }
    },
    // 选择折扣
    choosediscountclick(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var item = e.currentTarget.dataset.item;
        that.setData({
            discountsId: item.id,
            discount_name: item.frist_name + item.second_name,
            discountsIndex: index,
            discountsPrice: item.price
        })
    },
    // 获取所选课程
    cateCourse() {
        var that = this;
        var formData = {
            pid: that.data.cate_id,
            area_id: that.data.area_id,
            page:100,
            // user_id: that.data.uc.user_id,
        }
        share.wxRequest('crmapi/Market/cateCourse', formData, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data.lists;
                let arr=that.data.chooselist
                arr.forEach(val => {
                    if(val.id==that.data.cate_id){
                        val.courseList=data
                    }
                });
                var newarr = [];
                for (var i = 0; i < data.length; i++) {
                    newarr.push(data[i]);
                }
                that.setData({
                    chooselist: arr,
                    courseArray: newarr,
                    courselist: newarr,
                    ggelist: [],
                    pay_type: 1, //1一次付清
                    downpayment: 0, //首付金额
                })
                if (Number(data.length) < 1) {
                    wx.showToast({
                        title: '该分类下暂无课程',
                        icon: 'none'
                    })
                }
            }

        })
    },
    inputsearch() {
        var that = this;
        // console.log(8999)
        that.setData({
            is_search: 2,
            regin1: '',
            regin2: '',
            chooselist: [],
            "multiIndex[0]":-1,
            "multiIndex[1]": -1,
            cate_id: '',
            cart_title: '',
            courseIndex: [-1, -1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: 1,
            is_stepid: 1,
        })
        that.keywordcourse();
    },
    keywordcourse() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        share.wxRequest('crmapi/user/keyword', { status: 2, page: 1, limit: 100 }, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data.lists;
                that.setData({
                    keywordlists: data
                })
            }
            wx.hideLoading();
        })
    },
    keyclick(e) {
        var that = this;
        var item = e.target.dataset.item;
        that.areaCourse(item.name)
            // console.log(e)
            // console.log(item)
    },
    // 点击生成订单
    // ordersclick() {
    //     var that = this;
    //     var discountsIndex = that.data.discountsIndex;
    //     if (Number(discountsIndex) <= -1) {
    //         wx.showToast({
    //             title: '请选择折扣',
    //             icon: 'none'
    //         })
    //     } else {
    //         that.setData({
    //             is_scorder: 2
    //         })
    //     }
    // },
    // 点击搜索课程名称
    formSubmit(e) {
        var that = this;
        var keycourseall = e.detail.value.keycourseall;
        that.areaCourse(keycourseall)
    },
    areaCourse(name) {
        var that = this;
        if (name == '') {
            wx.showToast({
                title: '请输入关键字',
                icon: 'none'
            })
        } else {
            wx.showLoading({
                title: '加载中~',
            })
            share.wxRequest('crmapi/user/areaCourse', {
                keyword: name,
                area_id: that.data.area_id,
                user_id: that.data.uc.user_id,
                limit: 100
            }, function(res) {
                if (Number(res.data.errcode) == 0) {
                    var data = res.data.data;
                    if (Number(data.length) <= 0) {
                        wx.showToast({
                            title: '该搜索下暂无课程',
                            icon: 'none'
                        })
                    } else {
                        that.setData({
                            courseArray: data,
                            is_search: 1,
                            is_stepid: 2,
                            is_searchid: 2,
                        })
                    }
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
                wx.hideLoading();
            })
        }
    },

    // getDiscountPrice() {
    //     var that = this;
    //     var formData = {
    //         course_id: that.data.course_id, //	是	int	课程ID
    //         discount_id: that.data.discountsId, //	int	折扣ID
    //         specs_id: '', //	否	int	规格ID
    //     }
    //     share.wxRequest('crmapi/user/getDiscountByPrice', formData, function(res) {
    //         if (Number(res.data.errcode) == 0) {
    //             var data = res.data.price;
    //             // console.log(data);
    //             that.setData({
    //                 discountsPrice: Number(data),
    //             })
    //         }
    //     })
    // },


    coursetap() {
        var that = this;
        that.setData({
            showerca: true,
        })
    },
    /// 双击
    doubleTap: function(e) {
        var that = this
        that.setData({
            showerca: false,
        })
    },

    // 用户点击右上角分享
    onShareAppMessage: function() {
        return {
            title: CONFIG.SHARETITLE,
            path: CONFIG.PATHLOGIN
        }
    },

    // 提交
    bindFormSubmit: function(e) {
        let that = this;
        var textarea = e.detail.value.textarea;
        var e1 = e.detail.value;
        var uc = that.data.uc;
        var reg = /(^[-+]?[1-9]\d*(\.\d{1,2})?$)|(^[-+]?[0]{1}(\.\d{1,2})?$)/;
        var re = /^[\u4e00-\u9fa5a-z]+$/gi; //只能输入汉字和英文字母
        if (Number(that.data.courselist.length) != 0) {
            // console.log(that.data.regin)
            if (Number(that.data.area_index) == 0) {
                wx.showToast({
                    title: '请选择校区',
                    icon: 'none'
                })
            }
            if (that.data.cate_id == '') {
                wx.showToast({
                    title: '请选择课程分类',
                    icon: 'none'
                })
            } else if (that.data.courseregin1 == '') {
                wx.showToast({
                    title: '请选择所选课程',
                    icon: 'none'
                })
            }
            //  else if((Number(that.data.is_photo) == 1&&!that.data.photo)&&(Number(that.data.is_photo) == 1&&!that.data.uc.photo)){
            //     wx.showToast({
            //         title: '当前课程需要上传电子证件照，请上传后再提交',
            //         icon: 'none'
            //     })
            // }
            else {
                if (that.data.discountsId != '' && Number(that.data.discountsId) != 0) {
                    if (Number(uc.account_paid) > Number(that.data.discountsPrice)) {
                        if (e1.returnprice == '' || Number(e1.returnprice) <= 0) {
                            wx.showToast({
                                title: '请输入需退金额',
                                icon: 'none'
                            })
                        } else if (Number(uc.account_paid) < Number(e1.returnprice)) {
                            wx.showToast({
                                title: '需退金额不能大于原付款',
                                icon: 'none'
                            })
                        } 
                        // else if (Number(that.data.moneyfree_index) == 0) {
                        //     wx.showToast({
                        //         title: '请选择退款方式',
                        //         icon: 'none'
                        //     })
                        // }
                         else {
                            that.insertUserCourse(e1);
                        }
                    } else {
                        that.insertUserCourse(e1);
                    }
                } else {
                    if (Number(uc.account_paid) > Number(that.data.y_price)) {
                        if (e1.returnprice == '' || Number(e1.returnprice) <= 0) {
                            wx.showToast({
                                title: '请输入需退金额',
                                icon: 'none'
                            })
                        } else if (Number(uc.account_paid) < Number(e1.returnprice)) {
                            wx.showToast({
                                title: '需退金额不能大于原付款',
                                icon: 'none'
                            })
                        } 
                        // else if (Number(that.data.moneyfree_index) == 0) {
                        //     wx.showToast({
                        //         title: '请选择退款方式',
                        //         icon: 'none'
                        //     })
                        // } 
                        else {
                            that.insertUserCourse(e1);
                        }
                    } else {
                        that.insertUserCourse(e1);
                    }
                }
            }


        } else {
            wx.showToast({
                title: '该分类下没有课程，暂不能提交',
                icon: 'none'
            })
        }

    },

    insertUserCourse(e1) {
        // console.log(filePath);
        var that = this;
        if (e1.textarea != '') {
            that.setData({
                butfalse: false,
            })
            wx.showLoading({
                title: '提交中~',
                mask: true
            })
            var uc = that.data.uc;
            var moneyfree_index = that.data.moneyfree_index;
            // console.log(that.data.discountsId)
            if (that.data.discountsId == '' || Number(that.data.discountsId) <= 0) {
                var discounts = Number(that.data.y_price)
            } else {
                var discounts = Number(that.data.discountsPrice)
            }
            // if(Number(moneyfree_index)>=2||Number(moneyfree_index)==0){
            var banknamw = '';
            var banknum = '';
            var bank_code = '';
            var bank_name = '';
            // var money = 0;
            var money = e1.returnprice?e1.returnprice:0
                //   }else{
                //     var banknamw = e1.banknamw;
                //     var banknum = e1.banknum;
                //     var bank_code = that.data.bankarr[that.data.bank_index].bank_code;
                //     var bank_name = that.data.bankarr[that.data.bank_index].title;
                //     var money = e1.returnprice
                //   }
                // app.globalData.admin_id
                let discount_id=''
                let discount_name=''
                let is_train=that.data.is_train
                let discount_id2=''
                let discount_id3=''
                let product=[]
                if(that.data.cate_id1=='162'||that.data.cate_id1==162){
                    if(that.data.product.length==1){
                        discount_id=that.data.attrInfo.id
                        product=[]
                        discount_id2=''
                        discount_id3=''
                        is_train=is_train
                        discount_name=that.data.attrInfo.frist_name
    
                    }else{
                        if(is_train==3){
                            discount_id2=that.data.attrInfo.id
                            discount_id3=''
                        }else{
                            discount_id2=''
                            discount_id3=that.data.attrInfo.id
                        }
                        discount_id=0
                        product=that.data.product
                        discount_name=that.data.attrInfo.frist_name
                    }
                }else{
                    discount_id=that.data.attrInfo.id
                    product=[]
                    discount_id2=''
                    is_train=0
                    discount_name=that.data.attrInfo.frist_name+that.data.attrInfo.second_name
    
                }
            var params = {
                    admin_id:app.globalData.admin_id,
                    type: 2,
                    id: that.data.stu_id,
                    area_id: that.data.area_id,
                    course_id: that.data.course_id, //	是	int	课程ID
                    money: money,
                    pay_type: that.data.moneyfree[that.data.moneyfree_index].index,
                    specs_id: that.data.specs_id, //	否	int	规格ID
                    discount_id: discount_id, //	否	int	折扣ID
                    discounts: that.data.attrInfo.price, //	是	int	折扣价
                    y_discounts: that.data.y_price, //	是	int	课程原价
                    reason: e1.textarea, //	是	string	备注
                    bank_user: banknamw,
                    bank_num: banknum,
                    bank_code: bank_code,
                    bank_name: bank_name,
                    photo:that.data.uc.photo?that.data.uc.photo:that.data.photo,
                    discount_name:that.data.attrInfo.frist_name+that.data.attrInfo.second_name,
                    product:product,
                    discount_name:discount_name,
                    is_train:is_train,
                    discount_id2:discount_id2,
                    discount_id3:discount_id3
                }
                 console.log(params)
            if (e1.textarea != '') {
                that.setData({
                    textsan: e1.textarea,
                })
            }
            that.insertUserCourseNew(params);
        } else {

            wx.showToast({
                title: '请填写申请原因',
                icon: 'none'
            })
        }

    },


    // 线上
    insertUserCourseNew(params) {
        var that = this;
        share.wxRequest('crmapi/user/submitAudit', params, function(res) {
            var data = res.data;
            if (Number(data.errcode) == 0) {
                wx.setStorageSync('audit_id', res.data.data.audit_id);
                that.setData({
                    showfalse1: true,
                })
                wx.hideLoading();
            } else {
                wx.hideLoading();
                that.setData({
                    text: data.msg,
                    showfalse: true,
                })

            }
        })
    },

    sureclick() {
        var that = this;
        that.setData({
            butfalse: true,
            showfalse: false,
        })
    },
    sureclick1() {
        var that = this;
        wx.navigateBack();
    },
    linkreportclick(e){
        console.log(e);
        var id = e.currentTarget.dataset.id || e.target.dataset.id;
        var courselist=this.data.courselist
        // courselist[0].type=id
        this.setData({
            childtype:id,
            courselist:courselist
        })
    },
    preView(){
        let img=this.data.photo?this.data.photo:this.data.uc.photo
        var jiaoyanCode = this.data.imgUrl+img;
        share.sendImage(jiaoyanCode);
    },
    uploadImage3: function() {
        let that = this;
        wx.showLoading({
            title: '上传中~',
        })
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: function(res) {
                wx.showLoading({
                    title: '上传中~',
                })
                console.log(res);
                var photoSrc = res.tempFilePaths[0];
                share.uploadpic(photoSrc, function(res) {
                    var path = res.data.path;
                    console.log(path);
                    that.setData({
                        photo: path
                    })
                    wx.showToast({
                        title: '上传成功',
                        icon: 'none'
                    })
                    wx.hideLoading()
                }, function(res) {
                    wx.showToast({
                        title: '上传失败，请重新上传',
                        icon: 'none'
                    })
                    wx.hideLoading()
                })
            }
        })
    },

})